%save 2D plot data to text files
input_fname = fullfile('0ms_HoldHeating_Ones_Nov1', '2017-11-01_Folders=047-060_struct.mat');

%load data
a = load(input_fname);
dset = DiffuseSet();
dset.loadStruct(a.AsStruct);

%write 2D images to text files
for ii = 1:length(dset.dfs_index_folders)
    index = dset.dfs_index_folders(ii);
    fname = sprintf('2d_avg_t=%dus_%s.dat', dset.Times(ii), dset.dfs(index).DatasetString);
    img = rot90(flipud(dset.dfs(index).Occs_ImgAvg));
    dlmwrite(fname, img, 'delimiter', '\t');
end

%write 2D normalization picture, no modulation
index = dset.dfs_index_normfolders(1);
fname = sprintf('2d_avg_norm_pic_%s.dat', dset.dfs(index).DatasetString);
img = rot90(flipud(dset.dfs(index).Occs_ImgAvg));
dlmwrite(fname, img, 'delimiter', '\t');

%write normalization azimuthal average
azavg_fname = sprintf('az_avg_norm_pic_%s.dat', dset.dfs(index).DatasetString);
titles = {'radius(sites)', '<n_1>', '<n_1>_unc'};
azavg_dat = [dset.dfs(index).RadialPos, dset.dfs(index).Occs_AzAvg, dset.dfs(index).Occs_AzAvgUnc];

fid = fopen(azavg_fname, 'w');
for ii = 1:length(titles)-1
    fprintf(fid, '%s\t', titles{ii});
end
fprintf(fid, '%s\n', titles{end});
fclose(fid);
dlmwrite(azavg_fname, azavg_dat, 'delimiter', '\t', '-append');

%save ellipse data
ellipse_dat_fname = sprintf('roi_ellipse_dat_%s.txt', dset.DescriptStr);
titles = {'cx', 'cy', 'ax', 'ay', 'angle'};
sx = dset.dfs(1).GaussFitParams(3);
sy = dset.dfs(2).GaussFitParams(4);
aspect_ratio = sx/sy;
cutoff_rad = 15;
ax = cutoff_rad * sqrt(aspect_ratio);
ay = cutoff_rad / sqrt(aspect_ratio);
ellipse_dat = [dset.dfs(1).Cx_AzAvg, dset.dfs(1).Cy_AzAvg, ax, ay, dset.dfs(1).GaussFitParams(6)];

fid = fopen(ellipse_dat_fname, 'w');
for ii = 1:length(titles)-1
    fprintf(fid, '%s\t', titles{ii});
end
fprintf(fid, '%s\n', titles{end});
fclose(fid);
dlmwrite(ellipse_dat_fname, ellipse_dat, 'delimiter', '\t', '-append');

